from datetime import datetime

from app import db


class User(db.Model):
    """会员"""

    __tablename__ = "user"
    id = db.Column(db.Integer, primary_key=True)  # 编号
    name = db.Column(db.String(100), unique=True)  # 昵称
    pwd = db.Column(db.String(100))  # 密码
    email = db.Column(db.String(100), unique=True)  # 邮箱
    phone = db.Column(db.String(11), unique=True)  # 手机号码
    info = db.Column(db.Text)  # 个人信息
    face = db.Column(db.String(255), unique=True)  # 头像
    add_time = db.Column(db.DateTime, index=True, default=datetime.now())  # 注册时间
    uuid = db.Column(db.String(255), unique=True)  # 唯一用户id

    user_logs = db.relationship("UserLog", backref="user")  # 会员日志的外键关联
    comments = db.relationship("Comment", backref="user")  # 评论的外键关联
    movie_cols = db.relationship("MovieCol", backref="user")  # 电影收藏的外键关联

    def __repr__(self):
        return "<user {}>".format(self.name)


class UserLog(db.Model):
    """会员登录日志"""

    __tablename__ = "user_log"
    id = db.Column(db.Integer, primary_key=True)  # 编号
    user_id = db.Column(db.Integer, db.ForeignKey("user.id"))  # 所属会员
    ip = db.Column(db.String(100))  # 登录 ip
    add_time = db.Column(db.DateTime, index=True, default=datetime.now())  # 登录时间

    def __repr__(self):
        return "<user_log {}>".format(self.id)


class Tag(db.Model):
    """标签"""

    __tablename__ = "tag"
    id = db.Column(db.Integer, primary_key=True)  # 编号
    name = db.Column(db.String(100), unique=True)  # 名称
    add_time = db.Column(db.DateTime, index=True, default=datetime.now())  # 添加时间

    user_logs = db.relationship("Movie", backref="tag")  # 电影的外键关联

    def __repr__(self):
        return "<tag {}>".format(self.name)


class Movie(db.Model):
    """电影"""

    __tablename__ = "movie"
    id = db.Column(db.Integer, primary_key=True)  # 编号
    title = db.Column(db.String(255), unique=True)  # 标题
    url = db.Column(db.String(255), unique=True)  # url
    info = db.Column(db.Text)  # 电影介绍
    logo = db.Column(db.String(255), unique=True)  # logo
    star = db.Column(db.SmallInteger)  # 评价星级
    play_num = db.Column(db.BigInteger)  # 播放次数
    comment_num = db.Column(db.BigInteger)  # 评论数
    tag_id = db.Column(db.Integer, db.ForeignKey("tag.id"))  # 标签id
    area = db.Column(db.String(255))  # 上映地区
    release_time = db.Column(db.Date)  # 上映时间
    length = db.Column(db.String(100))  # 电影时长
    add_time = db.Column(db.DateTime, index=True, default=datetime.now())  # 添加时间

    comments = db.relationship("Comment", backref="movie")  # 评论的外键关联
    movie_cols = db.relationship("MovieCol", backref="movie")  # 电影收藏的外键关联

    def __repr__(self):
        return "<movie {}>".format(self.title)


class Preview(db.Model):
    """上映预告"""

    __tablename__ = "preview"
    id = db.Column(db.Integer, primary_key=True)  # 编号
    title = db.Column(db.String(255), unique=True)  # 标题
    logo = db.Column(db.String(255), unique=True)  # logo
    add_time = db.Column(db.DateTime, index=True, default=datetime.now())  # 添加时间

    def __repr__(self):
        return "<preview {}>".format(self.title)


class Comment(db.Model):
    """评论"""

    __tablename__ = "comment"
    id = db.Column(db.Integer, primary_key=True)  # 编号
    content = db.Column(db.Text)  # 评论内容

    movie_id = db.Column(db.Integer, db.ForeignKey("movie.id"))
    user_id = db.Column(db.Integer, db.ForeignKey("user.id"))

    def __repr__(self):
        return "<comment {}>".format(self.id)


class MovieCol(db.Model):
    """电影收藏"""

    __tablename__ = "movie_col"
    id = db.Column(db.Integer, primary_key=True)  # 编号
    add_time = db.Column(db.DateTime, index=True, default=datetime.now())  # 添加时间

    movie_id = db.Column(db.Integer, db.ForeignKey("movie.id"))
    user_id = db.Column(db.Integer, db.ForeignKey("user.id"))

    def __repr__(self):
        return "<movie_col {}>".format(self.id)


class Auth(db.Model):
    """权限"""

    __tablename__ = "auth"
    id = db.Column(db.Integer, primary_key=True)  # 编号
    name = db.Column(db.String(100), unique=True)  # 名称
    url = db.Column(db.String(255), unique=True)  # url
    add_time = db.Column(db.DateTime, index=True, default=datetime.now())  # 添加时间

    def __repr__(self):
        return "<auth {}>".format(self.name)


class Role(db.Model):
    """角色"""

    __tablename__ = "role"
    id = db.Column(db.Integer, primary_key=True)  # 编号
    name = db.Column(db.String(100), unique=True)  # 名称
    auth = db.Column(db.String(600))
    add_time = db.Column(db.DateTime, index=True, default=datetime.now())  # 添加时间

    admins = db.relationship("Admin", backref="role")  # 管理员的外键关联

    def __repr__(self):
        return "<role {}>".format(self.name)


class Admin(db.Model):
    """管理员"""

    __tablename__ = "admin"
    id = db.Column(db.Integer, primary_key=True)  # 编号
    name = db.Column(db.String(100), unique=True)  # 管理员账号
    pwd = db.Column(db.String(100))  # 密码
    is_super = db.Column(db.BigInteger)  # 是否超级管理员；0-是，1-不是
    add_time = db.Column(db.DateTime, index=True, default=datetime.now())  # 添加时间

    role_id = db.Column(db.Integer, db.ForeignKey("role.id"))  # 所属角色

    admin_logs = db.relationship("AdminLog", backref="admin")  # 管理员登录日志的外键关联
    op_logs = db.relationship("OpLog", backref="admin")  # 管理员操作日志的外键关联

    def __repr__(self):
        return "<admin {}>".format(self.name)

    def check_pwd(self, pwd):
        """
        检测密码
        :param pwd:
        :return:
        """
        from werkzeug.security import check_password_hash
        return check_password_hash(self.pwd, pwd)



class AdminLog(db.Model):
    """管理员登录日志"""

    __tablename__ = "admin_log"
    id = db.Column(db.Integer, primary_key=True)  # 编号
    ip = db.Column(db.String(100))
    add_time = db.Column(db.DateTime, index=True, default=datetime.now())  # 添加时间

    admin_id = db.Column(db.Integer, db.ForeignKey("admin.id"))  # 所属管理员

    def __repr__(self):
        return "<admin log {}>".format(self.id)


class OpLog(db.Model):
    """管理员操作"""

    __tablename__ = "op_log"
    id = db.Column(db.Integer, primary_key=True)  # 编号
    ip = db.Column(db.String(100))
    reason = db.Column(db.String(500))  # 操作原因
    add_time = db.Column(db.DateTime, index=True, default=datetime.now())  # 添加时间

    admin_id = db.Column(db.Integer, db.ForeignKey("admin.id"))  # 所属管理员

    def __repr__(self):
        return "<option log {}>".format(self.id)
